package com.beyond.trace.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beyond.trace.domain.TraceScanStatistics;
import com.beyond.trace.domain.vo.*;
import com.beyond.trace.mapper.TraceScanStatisticsMapper;
import com.beyond.trace.service.TraceScanStatisticsService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 扫码统计;(t_trace_scan_statistics)表服务实现类
 * @author : zq
 * @date : 2023-1-6
 */
@Service
public class TraceScanStatisticsServiceImpl extends ServiceImpl<TraceScanStatisticsMapper, TraceScanStatistics> implements TraceScanStatisticsService {

    @Autowired
    private TraceScanStatisticsMapper scanStatisticsMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public HomeShowVo homeShow() {
        HomeShowVo homeShowVo = new HomeShowVo();
        ScanStatisticsVo scanStatistics = scanStatisticsMapper.getScanStatistics();//顶部扫码统计
        List<ScanSevenDaysCountVo> SevenDaysCountList = scanStatisticsMapper.selectSevenDaysCount();//查询近七天每日的统计
        Integer countAll = scanStatisticsMapper.countAll();//统计所有正常扫码数量
        List<ScanProvinceCountVo> scanProvinceCountList = scanStatisticsMapper.provinceCount();//扫码省份统计
        List<TraceScanStatistics> selectTenStripList = scanStatisticsMapper.selectTenStrip(); //查询最新十条
        List<ScanSevenDaysCountVo> thisWeekCountLists = scanStatisticsMapper.thisWeekCountList();//查询本周每日统计
        List<Date> dates = DateUtils.DateCount();
        List<ScanSevenDaysCountVo> weekCount = getWeekCount(thisWeekCountLists, dates).stream().sorted(Comparator.comparing(ScanSevenDaysCountVo::getTime)).collect(Collectors.toList());
        homeShowVo.setScanStatisticsVo(scanStatistics);
        homeShowVo.setSevenDaysCountList(SevenDaysCountList);
        homeShowVo.setCountAll(countAll);
        homeShowVo.setScanProvinceCountList(scanProvinceCountList);
        homeShowVo.setSelectTenStripList(selectTenStripList);
        homeShowVo.setThisWeekCountList(weekCount);
        return homeShowVo;

    }

    @Override
    public List<ScanProductCountVo> groupProduct() {
        return this.scanStatisticsMapper.groupProduct();
    }

    public List<ScanSevenDaysCountVo> getWeekCount(List<ScanSevenDaysCountVo> thisWeekCountLists,List<Date> dates) {
        List<ScanSevenDaysCountVo> resultList = new ArrayList<>();
        for (Date date : dates) {
            String format = sdf.format(date);
            List<ScanSevenDaysCountVo> collect = thisWeekCountLists.stream()
                    .filter(s->format.equals(sdf.format(s.getTime()))).collect(Collectors.toList());
            if (collect.size() == 0) {
                ScanSevenDaysCountVo scanSevenDaysCountVo = new ScanSevenDaysCountVo(date,0);
                resultList.add(scanSevenDaysCountVo);
            }else {
                resultList.add(collect.get(0));
            }
        }
        return resultList;
    }
}
